define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'notice/noticesendrecode/index' + location.search,
                    add_url: 'notice/noticesendrecode/add',
                    edit_url: 'notice/noticesendrecode/edit',
                    del_url: 'notice/noticesendrecode/del',
                    multi_url: 'notice/noticesendrecode/multi',
                    import_url: 'notice/noticesendrecode/import',
                    table: 'notice_event',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('类型'), searchList: Config.typeList, operate:'FIND_IN_SET', formatter: Table.api.formatter.label},
                        {field: 'name', title: __('标题'), operate: 'LIKE'},
                        {field: 'send_num', title: __('发送次数')},
                        {field: 'read_num', title: __('已读人数')},
                        {field: 'send_adminnum', title: __('发送人数')},
                        {field: 'admin.nickname', title: __('创建人')},
                        {field: 'visible_switch', title: __('状态'), searchList: {"0":__('Visible_switch 0'),"1":__('Visible_switch 1')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, visible: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'test',
                                    text: '发送',
                                    url: 'notice/noticesendrecode/send',
                                    classname: 'btn btn-xs btn-dialog btn-primary',
                                },
                                {
                                    name: 'copy',
                                    text: '复制',
                                    url: 'notice/noticesendrecode/edit?is_copy=1',
                                    classname: 'btn btn-xs btn-dialog btn-primary',
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'notice/noticesendrecode/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'notice/noticesendrecode/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'notice/noticesendrecode/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        send: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $("form[role=form] button[type=submit]").click(function (e){
                    var send = $(this).data("send");
                    console.log('bindevent', send)
                    if(send == 1){
                        Form.api.submit($("form[role=form]"), function(res) {
                            Fast.api.open("notice/noticesendrecode/send?ids="+res, "发送");
                            return false
                        })
                        return false
                    } else if (send == 2) {
                        Form.api.submit($("form[role=form]"), function(res) {
                            console.log('send', send, res,window)
                            // window.parent.layer.close(window.parent.layer.getFrameIndex(window.name)); // 关闭子弹框
                            window.parent.parent.location.reload()
                            // parent.layer.closeAll()
                            // $("#table").bootstrapTable('refresh');
                        })
                        return false
                    }
                    
                })
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});